package com.wcar.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.*;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.product.dao.*;
import com.wcar.product.service.RroductReviewService;
import com.wcar.product.vo.ProductReviewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RroductReviewServiceImpl implements RroductReviewService {
    @Autowired
    private ProductReviewDao productReviewDao;
    @Autowired
    private FrontUserDao frontUserDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private StoreUserDao storeUserDao;
    @Override
    public Result<Page<ProductReview>> select(ProductReviewVo productReviewVo) {
        Page<ProductReview> page = new Page<>(productReviewVo.getCurrentPage(), productReviewVo.getPageSize());
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper<>();
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<UserRole> userRoleQueryWrapper= new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id",user.getUserId());
        List<UserRole> userRoles = userRoleDao.selectList(userRoleQueryWrapper);
        ArrayList<Integer> roleIds = new ArrayList<>();
        for (UserRole userRole : userRoles) {
            roleIds.add(userRole.getRoleId());
        }
        QueryWrapper<StoreUser> storeUserQueryWrapper = new QueryWrapper<>();
        if(!roleIds.contains(1)){
            storeUserQueryWrapper.eq("user_id",user.getUserId());
        }
        List<StoreUser> storeUsers = storeUserDao.selectList(storeUserQueryWrapper);
        ArrayList<Integer> storeIds = new ArrayList<>();
        for (StoreUser storeUser:storeUsers){
            storeIds.add(storeUser.getStoreId());
        }
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(storeIds.size()==0){
            return new Result<>(201,"查询评价成功", new Page<>());
        }
        wrapper.eq("store_id",storeIds);
        List<Product> products = productDao.selectList(wrapper);
        ArrayList<Integer> productIds = new ArrayList<>();
        for (Product product : products) {
            productIds.add(product.getProductId());
        }
        if(productIds.size()>0){
            queryWrapper.in("tpr.product_id",productIds);
        }
        if (productReviewVo.getProductName() != null&&productReviewVo.getProductName()!="") {
            queryWrapper.like("product_name", productReviewVo.getProductName());
        }
        if(productReviewVo.getUserName()!=null&&productReviewVo.getUserName()!="") {
            queryWrapper.like("tfu.user_name", productReviewVo.getUserName());
        }
        Page<ProductReview> list = productReviewDao.getPage(page, queryWrapper);
        return new Result<>(200,"查询评价成功", list);
    }

    @Override
    public Result<List<FrontUser>> selectAllUser() {
        List<FrontUser> list = frontUserDao.selectList(null);
        return new Result<>(200,"查询用户成功",list);
    }
}
